/* SPDX-License-Identifier: GPL-2.0 */
/*
 * csp_gpadc.h - gpadc csp operation header
 *
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#ifndef __GPADC_CSP_H___
#define __GPADC_CSP_H___

#include <mach/csp.h>
#include "../common.h"

/* GPADC register offset */
#define GPADC_GPEN_OFF			(0x00000010)
#define GPADC_CDAT_OFF			(0x00000014)
#define GPADC_AINC_OFF			(0x00000020)
#define GPADC_AINIE_OFF			(0x00000024)
#define GPADC_AINIS_OFF			(0x00000028)
#define GPADC_AINIC_OFF			(0x0000002C)

#define GPADC_AIN_X_DAT(x)		(0x00000040 + (0x10 * (x)))
#define GPADC_AIN_X_HDAT(x)		(0x00000044 + (0x10 * (x)))
#define GPADC_AIN_X_TDAT(x)		(0x00000048 + (0x10 * (x)))

#define GPADC_TSC_OFF			(0x00000100)
#define GPADC_TSIE_OFF			(0x00000104)
#define GPADC_TSIS_OFF			(0x00000108)
#define GPADC_TSIC_OFF			(0x0000010C)

#define GPADC_TS_X_CDAT(x)		(0x00000120 + (0x10 * (x)))
#define GPADC_TS_X_DAT(x)		(0x00000124 + (0x10 * (x)))
#define GPADC_TS_X_UT(x)		(0x00000128 + (0x10 * (x)))
#define GPADC_TS_X_DT(x)		(0x0000012C + (0x10 * (x)))

#define GPADC_FIOC_OFF			(0x00000200)
#define GPADC_FPUD_OFF			(0x00000204)
#define GPADC_FDRV_OFF			(0x00000208)
#define GPADC_FDAT_OFF			(0x0000020C)

/* AINX enable bit */
#define AIN_X_EN_BIT(x)			BIT(x)

/* AINX threshold interrupt enable, pending status, clear pending bit */
#define AIN_X_TH_BIT(x)			BIT((4 * x))

/* AINX data interrupt enable, pending status, clear pending bit */
#define AIN_X_DATA_BIT(x)		BIT(1 + (4 * x))

#define AIN_PEND_ALL	(AIN_X_TH_BIT(0) | AIN_X_DATA_BIT(0) |\
			AIN_X_TH_BIT(1) | AIN_X_DATA_BIT(1) |\
			AIN_X_TH_BIT(2) | AIN_X_DATA_BIT(2) |\
			AIN_X_TH_BIT(3) | AIN_X_DATA_BIT(3) |\
			AIN_X_TH_BIT(4) | AIN_X_DATA_BIT(4) |\
			AIN_X_TH_BIT(5) | AIN_X_DATA_BIT(5) |\
			AIN_X_TH_BIT(6) | AIN_X_DATA_BIT(6) |\
			AIN_X_TH_BIT(7) | AIN_X_DATA_BIT(7))

#define AIN_INT_CLR_ALL			AIN_PEND_ALL


/* Tempareture sensor down threshold, up threshold, data */
#define TS_X_DT_BIT(x)			BIT((4 * x))
#define TS_X_UT_BIT(x)			BIT(1 + (4 * x))
#define TS_X_DATA_BIT(x)		BIT(2 + (4 * x))
#define TS_X_MSK(x)			(0x7 << (4 * x))

#define TS_PEND_ALL	(TS_X_MSK(0) | TS_X_UT_BIT(1) |\
			TS_X_MSK(2) | TS_X_UT_BIT(3) |\
			TS_X_MSK(4) | TS_X_UT_BIT(5) |\
			TS_X_MSK(6) | TS_X_UT_BIT(7))

#define TS_INT_CLR_ALL			TS_PEND_ALL

typedef enum {
	GPADC_SRC_1KHZ = 0x0,
	GPADC_SRC_512HZ = 0x1,
	GPADC_SRC_256HZ = 0x2,
	GPADC_SRC_128HZ = 0x3,
	GPADC_SRC_1MHZ = 0x5,
} AIN_SAMPLE_RATE;

typedef enum {
	SAMPLE_RATE_8HZ = 0x0,
	SAMPLE_RATE_4HZ = 0x1,
	SAMPLE_RATE_2HZ = 0x2,
	SAMPLE_RATE_1HZ = 0x3,
} SENSOR_SAMPLE_RATE;

typedef enum {
	SENSOR_CHAN_0 = 0,
	SENSOR_CHAN_1 = 1,
	SENSOR_CHAN_2 = 2,
	SENSOR_CHAN_3 = 3,
	SENSOR_CHAN_4 = 4,
	SENSOR_CHAN_5 = 5,
	SENSOR_CHAN_6 = 6,
	SENSOR_CHAN_7 = 7,
	SENSOR_CHAN_NUM,
} SENSOR_CHAN;

void csp_gpadc_set_en(void *base, u32 en);
void csp_gpadc_set_cali_en(void *base, u32 en);
u32 csp_gpadc_get_cail_data(void *base);
/* enable debug interface, calibration LDO */
void csp_set_sample_time(void *base);
void csp_set_ldo(void *base);

/* AIN */
u32 csp_ain_low_voltage_det(void *base);
void csp_ain_set_sample_rate(void *base, AIN_SAMPLE_RATE t);
void csp_ain_set_sda_en(void *base, u32 en);

void csp_ain_set_en(void *base, u32 source, u32 en);
void csp_ain_set_int_en(void *base, u32 source, u32 en);
u32 csp_ain_get_int_pend(void *base);
void csp_ain_clr_int_pend(void *base, u32 source);

u32 csp_ain_get_data(void *base, u32 chan);
u32 csp_ain_get_sh_data(void *base, u32 chan);
void csp_ain_set_th_data(void *base, u32 chan, u32 th);
void csp_ain_set_data_int_en(void *base, u32 source, u32 en);

/* Temperature Sensor */
void csp_sensor_set_sample_rate(void *base, SENSOR_SAMPLE_RATE sr);
void csp_sensor_set_sda_en(void *base, u32 en);
void csp_sensor_set_en(void *base, u32 en);

void csp_sensor_set_int_en(void *base, u32 source, u32 en);
u32 csp_sensor_get_int_pend(void *base, u32 source);
void csp_sensor_clr_int_pend(void *base, u32 source);

void csp_ain_init_output(void *base, u32 chan, u32 en);
void csp_ain_set_output(void *base, u32 chan, u32 en);
int csp_ain_get_output(void *base);

/* thermal interface */
long csp_sensor_get_temp(int id, void __iomem *base, int slope, int offset);
int csp_calibration_offset(int id);
int csp_get_slope(int id);

#endif /* __GPADC_CSP_H___ */
